package com.konsung.provide;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.konsung.bean.CheckData;
import com.konsung.util.JsonUtil;
import com.konsung.util.constant.GlobalConstant;

/**
 * 内容提供读取者
 * 主要用于读取健康档案中的数据
 */
public class ProviderReader {

    /**
     * 获取设备号-从厂家维护中获取
     * @param context 上下文
     * @return 设备号
     */
    public static String getDeviceCode(Context context) {
        //获取设备号
        Uri uri = Uri.parse(GlobalConstant.DEVICE_CODE);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        String deviceCode = GlobalConstant.DEVICE_CODE_DEFAULT;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                deviceCode = cursor.getString(cursor.getColumnIndex(GlobalConstant.PATH));
            }
            cursor.close();
        }

        return deviceCode;
    }

    /**
     * 获取身份证号码是否强校验-从厂家维护中获取
     * @param context 上下文
     * @return 是否强校验 1校验，0不校验
     */
    public static boolean getIdCardCheckout(Context context) {
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_IDCARD_VERIFY);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        int config = 1;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getInt(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
        }
        return config == 1;
    }

    /**
     * 获取云服务器上传地址
     * @param context 上下文
     * @return 云服务器上传地址
     */
    public static String getUploadCloudPath(Context context) {
        //获取设备号
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_UPLOAD_CLOUD);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        String string = "";
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                string = cursor.getString(cursor.getColumnIndex(GlobalConstant.PATH));
            }
            cursor.close();
        }
        return string;
    }

    /**
     * 获取第三方服务器上传地址
     * @param context 上下文
     * @return 第三方服务器上传地址
     */
    public static String getUploadThirdpartyPath(Context context) {
        //获取设备号
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_UPLOAD_THIRDPARTY);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        String string = "";
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                string = cursor.getString(cursor.getColumnIndex(GlobalConstant.PATH));
                if (TextUtils.isEmpty(string)) {
                    string = "";
                }
            }
            cursor.close();
        }
        return string.replace("http://", "");
    }
    /**
     * 获取第三方服务器上传后缀
     * @param context 上下文
     * @return 第三方服务器上传后缀
     */
    public static String getUploadExitPath(Context context) {
        //获取设备号
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_UPLOAD_THIRDPARTY_EXIT);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        String string = "";
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                string = cursor.getString(cursor.getColumnIndex(GlobalConstant.SERVICE_SUFFIX));
            }

            cursor.close();
        }
        return string;
    }

    /**
     * 获取设备配置项-从厂家维护中获取
     * @param context 上下文
     * @return 配置项 32位
     */
    public static int getDeviceConfig(Context context) {
        //获取配置项
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_DEVICE_CONFIG);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        int config = GlobalConstant.DEVICE_CONFIG;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getInt(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
        }
        return config;
    }

    /**
     * 获取不依靠设备的测量项配置项(身高体重)-从厂家维护中获取
     * @param context 上下文
     * @return 配置项 32位
     */
    public static int getMeasureConfig(Context context) {
        //获取配置项
        Uri uri = Uri.parse(GlobalConstant.QUICK_CHECK_HEIGHT_WIGHT_CONFIG);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        int config = 0;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getInt(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
        }
        return config;
    }

    /**
     * 获取尿常规测量项配置-从厂家维护中获取
     * @param context 上下文
     * @return 配置项 32位
     */
    public static int getUrtSetting(Context context) {
        //获取配置项
        Uri uri = Uri.parse(GlobalConstant.QUICK_CHECK_URT_SETTING);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        int config = 0;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getInt(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
        }
        return config;
    }

    /**
     * 获取获取当前登录医生对象
     * @param context 上下文
     * @return 当前登录医生对象
     */
    public static CheckData getCheckData(Context context) {
        //获取配置项
        Uri uri = Uri.parse(GlobalConstant.CURRENT_DOCTOR);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        String config = "";
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getString(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
            if (!TextUtils.isEmpty(config)) {
                try {
                    return JsonUtil.toObject(config, CheckData.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 获取自动上传字段
     * @param context 上下文
     * @return 是否自动上传
     */
    public static boolean getAutoUpload(Context context) {
        //获取自动上传
        Uri uri = Uri.parse(GlobalConstant.AUTHORITY_AUTO_UPLOAD);
        Cursor cursor = context.getContentResolver().query(uri, null, null, null,
                null);
        int config = 1;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                config = cursor.getInt(cursor.getColumnIndex(GlobalConstant.CONFIG));
            }
            cursor.close();
        }
        return config == 1;
    }
}
